package com.th.supcom.pds.auditprescription.client.widget;

import com.extjs.gxt.ui.client.Style.LayoutRegion;
import com.extjs.gxt.ui.client.Style.Orientation;
import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.event.MessageBoxEvent;
import com.extjs.gxt.ui.client.util.Margins;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.Dialog;
import com.extjs.gxt.ui.client.widget.Info;
import com.extjs.gxt.ui.client.widget.LayoutContainer;
import com.extjs.gxt.ui.client.widget.MessageBox;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.layout.BorderLayout;
import com.extjs.gxt.ui.client.widget.layout.BorderLayoutData;
import com.extjs.gxt.ui.client.widget.layout.RowData;
import com.extjs.gxt.ui.client.widget.layout.RowLayout;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.th.supcom.framework.web.gwt.client.widget.FcMessageBox;
import com.th.supcom.pds.auditprescription.client.service.AuditPrescripRpcAsync;
import com.th.supcom.pds.common.client.commom.PdsAuditPrescServiceFactory;
import com.th.supcom.pds.common.client.model.PdsSupplyModel;
import com.th.supcom.pds.common.enums.CheckFlagEnum;
import com.th.supcom.pds.common.utils.ClientUtils;
import com.th.supcom.pds.internal.client.model.PatientInfoModel;
import com.th.supcom.pds.internal.client.model.ResultModel;

/**
 * AuditPrescription主面板.
 * 
 * 处方审核面板.
 */
/**
 *
 */

public class AuditPrescripMainPanel extends ContentPanel
{

    private static final AuditPrescripRpcAsync S_ASYNC = PdsAuditPrescServiceFactory.getAuditPrescripService ();
    /** 定义处方状态已经病人信息相关控件 */
    private AuditPrescripFields m_auditPrescripFields;

    /** 定义处方详明细息表格. */
    private AuditPrescripInfoGridPanel m_auditPrescripInfoGridPanel;
    private ResultModel m_resultModel;

    /** 定义病人资料详单面板 */
    private PatientInfoPanel m_patientInfoPanel;
    /** 定义处方查询面板 */
    private AuditPrescripSearchPanel m_searchPanel;
    private final Dialog window;

    public AuditPrescripMainPanel ()
    {
        setStyleAttribute ("border", "0px");
        setStyleAttribute ("padding", "0px");
        init ();
        window = m_searchPanel.getRefuseWindow ();
        initAction ();

    }

    private void auditPrescripFieldValue (final PdsSupplyModel pdsSupplyModel)
    {
        m_auditPrescripFields.getPrescType ().setValue (pdsSupplyModel.getPrescClassifcationName ());
        m_auditPrescripFields.getControlType ().setValue (pdsSupplyModel.getPrescTypeName ());
        m_auditPrescripFields.getRepetition ().setValue (pdsSupplyModel.getPxAmount ());
        m_auditPrescripFields.getOrderedBy ().setValue (pdsSupplyModel.getOrderedByName ());
        m_auditPrescripFields.getDiagEmpName ().setValue (pdsSupplyModel.getEmpNameBegDoct ());
        final DateTimeFormat format = DateTimeFormat.getFormat ("yyyy-MM-dd");
        m_auditPrescripFields.getEnterTime ().setValue (format.format (pdsSupplyModel.getCreateDate ()));
    }

    /**
     * false:不清空处方输入框 true:清空处方输入框
     * 
     * @param clearPrescNo
     */
    public void clearInfo (final boolean clearPrescNo)
    {
        if (clearPrescNo)
        {
            m_searchPanel.getPrescription ().clear ();
            m_searchPanel.getPrescription ().focus ();
        }
        else
        {
            m_searchPanel.getPrescription ().focus ();
        }
        m_auditPrescripInfoGridPanel.getStore ().removeAll ();
        m_auditPrescripFields.getPrescType ().clear ();
        m_auditPrescripFields.getControlType ().clear ();
        m_auditPrescripFields.getRepetition ().clear ();
        m_auditPrescripFields.getOrderedBy ().clear ();
        m_auditPrescripFields.getDiagEmpName ().clear ();
        m_auditPrescripFields.getEnterTime ().clear ();
        m_auditPrescripFields.getPrescSource ().clear ();
        m_auditPrescripFields.getPatientId ().clear ();
        m_auditPrescripFields.getPatientName ().clear ();
        m_auditPrescripFields.getChargeTypeName ().clear ();
        m_auditPrescripFields.getWatchTimes ().clear ();
        m_auditPrescripFields.getDeptAdmissionDate ().clear ();
        m_auditPrescripFields.getDeptAdmissionName ().clear ();
        m_auditPrescripFields.getDeptAdmissionDays ().clear ();
        m_auditPrescripFields.getBedNo ().clear ();
    }

    private void forTextFieldIsNull ()
    {
        MessageBox.alert ("提示", "处方号不能为空", new Listener <MessageBoxEvent> ()
        {
            @Override
            public void handleEvent (final MessageBoxEvent be)
            {
                m_searchPanel.getPrescription ().focus ();
            }
        });
    }

    public AuditPrescripFields getAuditPrescripFields ()
    {
        return m_auditPrescripFields;
    }

    public AuditPrescripInfoGridPanel getAuditPrescripInfoGridPanel ()
    {
        return m_auditPrescripInfoGridPanel;
    }

    public PatientInfoPanel getPatientInfoPanel ()
    {
        return m_patientInfoPanel;
    }

    private void init ()
    {
        m_auditPrescripFields = new AuditPrescripFields ();
        this.setHeaderVisible (false);
        this.setBodyBorder (false);
        setLayout (new BorderLayout ());

        // 初始化上部面板
        final LayoutContainer auditPrescripNorthPanel = new LayoutContainer ();
        final BorderLayoutData northData = new BorderLayoutData (LayoutRegion.NORTH);
        northData.setSize (175);
        auditPrescripNorthPanel.setStyleAttribute ("border", "0px");
        auditPrescripNorthPanel.setStyleAttribute ("padding", "0px");
        auditPrescripNorthPanel.setStyleAttribute ("margin", "0px");
        auditPrescripNorthPanel.setLayout (new BorderLayout ());
        /** 处方查询，显示面板. */
        m_searchPanel = new AuditPrescripSearchPanel ();
        final BorderLayoutData northData2 = new BorderLayoutData (LayoutRegion.NORTH);
        northData2.setMargins (new Margins (1, 4, 1, 4));
        northData2.setSize (50);
        auditPrescripNorthPanel.add (m_searchPanel, northData2);

        /** 定义处方状态以及患者基本信息面板 */
        final LayoutContainer prescStatusAndPatiInfo = new LayoutContainer ();
        prescStatusAndPatiInfo.setHeight (120);
        prescStatusAndPatiInfo.setLayout (new RowLayout (Orientation.HORIZONTAL));
        /** 处方患者详细信息面板. */
        m_patientInfoPanel = new PatientInfoPanel (m_auditPrescripFields);
        /** 定义处方状态面板 */
        final AuditPrescripStatusPanel m_statusPanel = new AuditPrescripStatusPanel (m_auditPrescripFields);
        prescStatusAndPatiInfo.add (m_statusPanel, new RowData (0.5, 1, new Margins (1, 4, 1, 4)));
        prescStatusAndPatiInfo.add (m_patientInfoPanel, new RowData (0.5, 1, new Margins (1, 4, 1, 2)));
        final BorderLayoutData centerData2 = new BorderLayoutData (LayoutRegion.CENTER);
        auditPrescripNorthPanel.add (prescStatusAndPatiInfo, centerData2);

        add (auditPrescripNorthPanel, northData);

        // 初始化中心面板
        m_auditPrescripInfoGridPanel = new AuditPrescripInfoGridPanel ();
        final BorderLayoutData centerData = new BorderLayoutData (LayoutRegion.CENTER);
        centerData.setMargins (new Margins (4));
        add (m_auditPrescripInfoGridPanel, centerData);

    }

    private void initAction ()
    {
        final AuditPrescripAction auditPrescripAction = new AuditPrescripAction ();
        auditPrescripAction.setAuditPrescripMainPanel (this);
        auditPrescripAction.setAuditPrescripSearchPanel (m_searchPanel);
        auditPrescripAction.initAction ();
    }

    /**
     * 通过处方操作
     */
    public void passPresc ()
    {
        if (m_auditPrescripInfoGridPanel.getStore ().getCount () == 0)
        {
            MessageBox.alert ("提示", "请先查询到处方信息再进行此操作。", new Listener <MessageBoxEvent> ()
            {
                @Override
                public void handleEvent (final MessageBoxEvent be)
                {
                    m_searchPanel.getPrescription ().focus ();
                }
            });
            return;
        }
        FcMessageBox.confirm ("提示", "该处方确定要通过审核吗？", new Listener <MessageBoxEvent> ()
        {
            @Override
            public void handleEvent (final MessageBoxEvent be)
            {
                final Button btn = be.getButtonClicked ();
                if (btn.getText ().equals ("否"))
                {
                    m_searchPanel.getPrescription ().focus ();
                    return;
                }
                final String operator = ClientUtils.geteEmpModel ().getEmpCode ();
                PdsSupplyModel pdsSupplyModel = m_resultModel.getPdsSupplyModels ().get (0);
                pdsSupplyModel.setUserIdCheck (operator);
                pdsSupplyModel.setUserNameCheck (operator);
                pdsSupplyModel.setOrgIdExec (ClientUtils.getOrgModel ().getOrgCode ());
                pdsSupplyModel.setOrgIdExecName (ClientUtils.getOrgModel ().getOrgName ());
                S_ASYNC.checkPresc (pdsSupplyModel, CheckFlagEnum.PASS.getEnumCode (), new AsyncCallback <String> ()
                {

                    @Override
                    public void onFailure (Throwable caught)
                    {
                        MessageBox.alert ("警告", caught.getMessage (), null);
                    }

                    @Override
                    public void onSuccess (String result)
                    {
                        window.hide ();
                        clearInfo (true);
                        Info.display ("提示", result);
                        return;
                    }
                });
            }
        }, Dialog.YES);

    }

    public void queryInfo (final PdsSupplyModel model)
    {
        if (model.getPrescNo () == null)
        {
            forTextFieldIsNull ();
            clearInfo (true);
            return;
        }
        m_auditPrescripInfoGridPanel.mask ("加载中");

        S_ASYNC.findPrescDetailInfoByPrescId (model, new AsyncCallback <ResultModel> ()
        {

            @Override
            public void onFailure (Throwable caught)
            {
                m_auditPrescripInfoGridPanel.unmask ();
                MessageBox.alert ("提示", caught.getMessage (), null);
                clearInfo (false);
                return;
            }

            @Override
            public void onSuccess (ResultModel result)
            {
                if (null == result || result.getPdsSupplyModels ().isEmpty ())
                {
                    m_auditPrescripInfoGridPanel.unmask ();
                    ClientUtils.alertMessageBox ("该处方号不存在,请重新输入。", m_searchPanel.getPrescription ());
                    return;
                }

                if (result.getPdsSupplyDetailModels ().isEmpty ())
                {
                    final PdsSupplyModel currentCondition = new PdsSupplyModel ();
                    currentCondition.setPrescNo (model.getPrescNo ());
                    // final PrescStatusQueryWindow window = new
                    // PrescStatusQueryWindow (currentCondition,
                    // m_searchPanel.getPrescription ());
                    m_auditPrescripInfoGridPanel.unmask ();
                    window.show ();
                    return;
                }

                m_resultModel = result;

                clearInfo (false);
                m_auditPrescripInfoGridPanel.setStoreData (result.getPdsSupplyDetailModels ());
                final PdsSupplyModel model = result.getPdsSupplyModels ().get (0);
                m_auditPrescripFields.getPrescSource ().setValue (model.getPrescSourceName ());
                auditPrescripFieldValue (model);
                setPatientInfo (result.getPatientInfoModel ());
                m_auditPrescripInfoGridPanel.unmask ();
                m_searchPanel.getPassBtn ().focus ();
            }
        });
    }

    /**
     * 查询患者信息
     * 
     * @param date
     */
    private void setPatientInfo (PatientInfoModel patientInfoModel)
    {
        m_auditPrescripFields.getPatientId ().setValue (patientInfoModel.getPatiId ());
        m_auditPrescripFields.getPatientName ().setValue (patientInfoModel.getName ());
        final DateTimeFormat format = DateTimeFormat.getFormat ("yyyy-MM-dd");
        m_auditPrescripFields.getChargeTypeName ().setValue (patientInfoModel.getChargeType ());
        m_auditPrescripFields.getWatchTimes ().setValue (patientInfoModel.getVisitId ());
        if (patientInfoModel.getVisitBeginDates () != null)
        {
            m_auditPrescripFields.getDeptAdmissionDate ()
                                 .setValue (format.format (patientInfoModel.getVisitBeginDates ()));
        }
        m_auditPrescripFields.getDeptAdmissionName ().setValue (patientInfoModel.getDeptCode ());
        m_auditPrescripFields.getDeptAdmissionDays ().setValue (patientInfoModel.getVisitDay ());
        m_auditPrescripFields.getBedNo ().setValue (patientInfoModel.getBedCode ());
    }

    /**
     * 拒绝处方操作
     */
    public void refusePresc ()
    {
        if (0 == m_auditPrescripInfoGridPanel.getStore ().getCount ())
        {
            MessageBox.alert ("提示", "请先查询到处方信息再进行此操作。", new Listener <MessageBoxEvent> ()
            {
                @Override
                public void handleEvent (final MessageBoxEvent be)
                {
                    m_searchPanel.getPrescription ().focus ();
                }
            });
            return;
        }
        window.show ();
    }

    public void setAuditPrescripFields (final AuditPrescripFields auditPrescripFields)
    {
        m_auditPrescripFields = auditPrescripFields;
    }

    public void setAuditPrescripInfoGridPanel (final AuditPrescripInfoGridPanel auditPrescripInfoGridPanel)
    {
        m_auditPrescripInfoGridPanel = auditPrescripInfoGridPanel;
    }

    public void setPatientInfoPanel (final PatientInfoPanel patientInfoPanel)
    {
        m_patientInfoPanel = patientInfoPanel;
    }

}